|
Writing a component editor
Any common control's editor (it can be called from the element's context menu or by double-clicking) creates an OnClick blank event's handler by default. This behavior can be replaced with writing a custom editor. In addition, the editor allows adding own items to the component's context menu. The basic class for all editors is described in the frxDsgnIntf file: TfrxComponentEditor = class (TObject) protected public function HasEditor: Boolean; virtual ; function Execute(Tag: Integer; Checked: Boolean): Boolean; virtual ; procedure GetMenuItems; virtual ; property Component: TfrxComponent readonly; property Designer: TfrxCustomDesigner readonly; end; If your editor does not create own items in the contextual menu, you will need to override two methods, i.e. “Edit” and “HasEditor.” The first method performs necessary actions (for example, displays a dialogue box) and returns “True,” if the component's state was modified. The “HasEditor” method should return “True” if your component has an editor. If it either returns “False” or you do not override this method, the editor will not be called. This becomes necessary, if your component does not have an editor and you wish to add items into the component's context menu. If the editor adds items into the context menu, you should override the “GetMenuItems” (in this method, you can create a menu with the help of calling in the AddItem function) and “Execute” (this method is called, when you select one of your items in the component's menu; response to the selected menu item should be described here) methods. Registration of the editor is performed via the procedure described in the “frxDsgnIntf” file: frxComponentEditors.Register(ComponentClass: TfrxComponentClass; The first parameter is the class' name, for which the editor is to be created. The second parameter is the name of the editor's class. Let us examine a simple editor for our common control, which will display a window with the name of our element and add the "Enabled" and "Visible" items to the element's context menu (when items are selected, the element's “Enabled” and “Visible” properties will change). The editor code, according to the agreement accepted in FastReport, can be placed in a file having the same name as the file with the very component's code, adding the Editor suffix (for example, frxBitBtnEditor.pas in our case). uses frxClass, frxDsgnIntf, frxBitBtn; type public function HasEditor: Boolean; override; function Execute(Tag: Integer; Checked: Boolean): Boolean; override; procedure GetMenuItems; override; end; function TfrxBitBtnEditor.Edit: Boolean; var begin { the Component property is the edited component. In this case, it is the TfrxBitBtnControl } c := TfrxBitBtnControl(Component); ShowMessage('This is ' + c.Name); end; function TfrxBitBtnEditor.HasEditor: Boolean; begin end; function TfrxBitBtnEditor.Execute(Tag: Integer; Checked: Boolean): Boolean; var begin c := TfrxBitBtnControl(Component); if Tag = 1 then else if Tag = 2 then end; procedure TfrxBitBtnEditor.GetMenuItems; var begin { the AddItem method's parameters: name of the menu item, its tag and Checked/Unchecked condition } AddItem('Enabled', 1, c.Enabled); AddItem('Visible', 2, c.Visible); end; initialization end. |